import random from './random.js';
import ksort from './ksort.js';
const key = '49833O6X8jede13thhbYam2TwIDAQAB*i~!@';
export default {
	secretdate: function() {
		var timestamp = new Date().getTime();
		var nonce = random.random_char(32);
		var data_for_sign = 'timestamp=' + timestamp + '&nonce=' + nonce;
		console.log('timestamp: ' + timestamp);
		console.log('nonce: ' + nonce);
		console.log('secretdate:' + data_for_sign);
		return data_for_sign;
	},
	getkey: function() {
		return key;
	},
	//字母排序：不区分大小写
	sortCharter(a, b) {
		if (a.toString().toUpperCase() > b.toString().toUpperCase()) {
			return 1;
		} else if (a.toString().toUpperCase() == b.toString().toUpperCase()) {
			return 0;
		} else {
			return -1;
		}
	},
	objKeySort(obj) { //排序的函数
		// var newkey = Object.keys(obj).sort(this.sortCharter);
		// //先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
		// var newObj = {}; //创建一个新的对象，用于存放排好序的键值对
		// for (var i = 0; i < newkey.length; i++) { //遍历newkey数组
		// 	newObj[newkey[i]] = obj[newkey[i]]; //向新创建的对象中按照排好的顺序依次增加键值对
		// }
		let newObj = ksort.ksort(
		  this,
		  obj
		)
		return newObj; //返回排好序的新对象
	},
	//创建sign
	create_sign(data) {

		console.log("1、排序前create_sign1");
		console.log(data);
		//1、排序
		data = this.objKeySort(data);
		console.log("1、排序后create_sign2");
		console.log(data);
		//2.拼接
		var txt = "";
		// var newkey = Object.keys(data);
		for (var k in data) { //遍历newkey数组
			txt += k + "=" + data[k] + "&";

		}
		//console.log("for内部===3.追加私钥appsecret");

		// console.log("appsecret=" + appsecret);
		txt = txt + this.getkey();
		//3.追加私钥appsecret
		console.log("paramstr:",txt);
		//4.md5加密
		return txt;
	}


}
